import houseApi from "@/api/store/house";

export default {
  name: "house",
  data() {
    return {
      formVisible: false,
      formTitle: "添加仓库",
      rules: {
        name: [
          { required: true, message: "请输入仓库名称", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
        capacity: [
          { required: true, message: "请输入仓库容量", trigger: "blur" }
        ],
        location: [
          { required: true, message: "请输入仓库地址", trigger: "blur" }
        ],
        manager: [
          { required: true, message: "请输入仓库管理员", trigger: "blur" }
        ],
      },
      listQuery: {
        current: 1,
        size: 20,
        criteria: {
          id: "",
          name: "",
          capacity: "",
          location: "",
          manager: "",
        },
      },
      list: null,
      total: 0,
      listLoading: true,
      selRow: {},
      form: {
        id: "",
        name: "",
        capacity: "",
        location: "",
        manager: "",
        num:""
      },
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.fetchData();
    },
    fetchData() {
      this.listLoading = true;
      houseApi
        .getList(this.listQuery)
        .then((response) => {
          this.list = response.data.records;
          this.listLoading = false;
          this.total = response.data.total;
        })
        .catch(() => {});
    },
    search() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    reset() {
      this.listQuery.criteria.name = "";
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleFilter() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleClose() {},
    fetchNext() {
      this.listQuery.current = this.listQuery.current + 1;
      this.fetchData();
    },
    fetchPrev() {
      this.listQuery.current = this.listQuery.current - 1;
      this.fetchData();
    },
    fetchPage(page) {
      this.listQuery.current = page;
      this.fetchData();
    },
    changeSize(limit) {
      this.listQuery.size = limit;
      this.fetchData();
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow;
    },

    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow;
    },
    resetForm() {
      this.form = {
        id: "",
        name: "",
        capacity: "",
        location: "",
        manager: "",
        num:""
      };
    },
    add() {
      this.resetForm();
      this.formTitle = "添加仓库";
      this.form.num = this.total +1
      this.formVisible = true;
    },
    edit() {
      if (this.checkSel()) {
        let form = Object.assign({}, this.selRow);
        this.form = form;
        this.formTitle = "修改仓库";
        this.formVisible = true;
      }
    },
    remove() {
      if (this.checkSel()) {
        let id = this.selRow.id;

        this.$confirm("确定删除该记录?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            houseApi
              .remove(id)
              .then((response) => {
                this.$message({
                  message: "删除成功",
                  type: "success",
                });
                this.fetchData();
              })
          })
          .catch(() => {});
      }
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true;
      }
      this.$message({
        message: "请选中操作项",
        type: "warning",
      });
      return false;
    },
    save() {
        let self = this
              this.$refs['form'].validate((valid) => {
                if (valid) {
                    let form = self.form
                    houseApi.save(form).then(response => {
                      this.$message({
                        message: '提交成功',
                        type: 'success'
                      })
                      this.fetchData()
                      this.formVisible = false
                    })
                  
                } else {
                  return false
                }
              })
    },
    editItem(row){
        this.selRow = Object.assign({},row);
        this.edit()
    },
    removeItem(row){
        this.selRow = Object.assign({},row);
        this.remove()
    },
    changeUserStatus(record){
          houseApi.changeStatus(record.id).then(response => {
            this.$message({
              message: '提交成功',
              type: 'success'
            })
            this.fetchData()
          })
        },
  },
};
